package library

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Response struct {
	ctx        *gin.Context
	statusCode int
}

// NewResponse 新建Response类
func NewResponse(ctx *gin.Context) *Response {
	return &Response{
		ctx:        ctx,
		statusCode: http.StatusOK,
	}
}

func (r *Response) WithResponseHttpCode(code int) *Response {
	r.statusCode = code
	return r
}

func (r *Response) send(message string, data any, code int) {
	r.ctx.JSON(r.statusCode, gin.H{
		"code":    code,
		"data":    data,
		"message": message,
	})
}

func (r *Response) sendAbort(message string, data any, code int) {
	r.ctx.AbortWithStatusJSON(r.statusCode, gin.H{
		"code":    code,
		"data":    data,
		"message": message,
	})
}

func (r *Response) Success(message string, data any) {
	r.send(message, data, 1)
}

func (r *Response) Error(message string) {
	r.send(message, nil, 0)
}

func (r *Response) ErrorAbort(message string) {
	r.sendAbort(message, nil, 0)
}
